package com.copa.l404;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author copa
 * @createDate 2022-09-05 15:35
 * @function BFS
 */
public class Test1 {

    public int sumOfLeftLeaves(TreeNode root) {
        if (root.left == root.right && root.left == null) {
            return 0;
        }
        int sum = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.add(node.left);
                    if (node.left.left == null && node.left.right == null) {
                        sum += node.left.val;
                    }
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(new Test1().sumOfLeftLeaves(root));

    }
}
